package com.example.orchid.coordinatorlayoutdemo.utils;

import android.text.TextUtils;

import com.example.orchid.coordinatorlayoutdemo.entity.BaseEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Objects;

/**
 * GsonUtil 解析工具类
 * Created by Orchid on 2015-10-29.
 */
public class GsonUtil {
    /**
     * 解析一个字符串，得到BaseEntity对象
     *
     * @param content
     * @param clazz
     * @return
     */
    public static BaseEntity getEntity(String content, Class<?> clazz) {

        if (TextUtils.isEmpty(content))
            return null;

        Gson gson = new GsonBuilder().create();

        try {
            BaseEntity baseEntity = (BaseEntity) gson.fromJson(content, clazz);
            return baseEntity;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 从对象转成json字符串
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        if (object == null)
            return null;

        Gson gson = new GsonBuilder().create();

        try {
            String json = gson.toJson(object);
            System.out.println(json);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
